package com.gpy.hospital.reception.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gpy.hospital.cms.dao.DoctorMapper;
import com.gpy.hospital.cms.dao.KeshiMapper;
import com.gpy.hospital.cms.pojo.Doctor;
import com.gpy.hospital.cms.pojo.Keshi;
import com.gpy.hospital.cms.vo.DoctorVo;
import com.gpy.hospital.common.ServerResponse;
import com.gpy.hospital.reception.service.IDoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author
 * @Date 2020/2/27 18:41
 * @Desc
 **/
@Service
public class DoctorServiceImpl implements IDoctorService {
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private KeshiMapper keshiMapper;

    @Override
    public List<Doctor> getDoctor() {
        // 随机去5个
        List<Doctor> doctors = doctorMapper.getRandom();
        return doctors;
    }

    @Override
    public PageInfo<Doctor> getByDoctorName(String doctorName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Doctor> doctors = doctorMapper.getByDoctorName(doctorName);
        return new PageInfo<>(doctors);
    }

    @Override
    public Doctor getById(Integer doctorId) {
        return doctorMapper.selectByPrimaryKey(doctorId);
    }

    @Override
    public ServerResponse saveDoctor(DoctorVo doctorVo) {
        int result = 0;
        List<String> chuzhen = new ArrayList<>();
        if (doctorVo.getA() == null) {
            chuzhen.add(String.valueOf(0));
        } else {
            chuzhen.add(String.valueOf(1));
        }
        if (doctorVo.getB() == null) {
            chuzhen.add(String.valueOf(0));
        } else {
            chuzhen.add(String.valueOf(1));
        }
        if (doctorVo.getC() == null) {
            chuzhen.add(String.valueOf(0));
        } else {
            chuzhen.add(String.valueOf(1));
        }
        if (doctorVo.getD() == null) {
            chuzhen.add(String.valueOf(0));
        } else {
            chuzhen.add(String.valueOf(1));
        }
        if (doctorVo.getE() == null) {
            chuzhen.add(String.valueOf(0));
        } else {
            chuzhen.add(String.valueOf(1));
        }
        if (doctorVo.getF() == null) {
            chuzhen.add(String.valueOf(0));
        } else {
            chuzhen.add(String.valueOf(1));
        }
        if (doctorVo.getG() == null) {
            chuzhen.add(String.valueOf(0));
        } else {
            chuzhen.add(String.valueOf(1));
        }
        if (doctorVo.getH() == null) {
            chuzhen.add(String.valueOf(0));
        } else {
            chuzhen.add(String.valueOf(1));
        }
        if (doctorVo.getI() == null) {
            chuzhen.add(String.valueOf(0));
        } else {
            chuzhen.add(String.valueOf(1));
        }
        if (doctorVo.getJ() == null) {
            chuzhen.add(String.valueOf(0));
        } else {
            chuzhen.add(String.valueOf(1));
        }
        if (doctorVo.getK() == null) {
            chuzhen.add(String.valueOf(0));
        } else {
            chuzhen.add(String.valueOf(1));
        }
        if (doctorVo.getL() == null) {
            chuzhen.add(String.valueOf(0));
        } else {
            chuzhen.add(String.valueOf(1));
        }
        if (doctorVo.getM() == null) {
            chuzhen.add(String.valueOf(0));
        } else {
            chuzhen.add(String.valueOf(1));
        }
        if (doctorVo.getN() == null) {
            chuzhen.add(String.valueOf(0));
        } else {
            chuzhen.add(String.valueOf(1));
        }
        Keshi keshi = keshiMapper.selectByPrimaryKey(doctorVo.getKeshiId());
        doctorVo.setKeshi(keshi.getName());
        if (doctorVo.getId() == null) {
            // 新添加
            doctorVo.setChuzhen(chuzhen.toString().replace(" ", ""));
            result = doctorMapper.insert(doctorVo);
        } else {
            doctorVo.setChuzhen(chuzhen.toString().replace(" ", ""));
            result = doctorMapper.updateByPrimaryKey(doctorVo);
        }
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("成功！");
        }
        return ServerResponse.createByErrorMessage("失败！");
    }

    @Override
    public List<Doctor> getByKeShiId(Integer keshiId) {
        return doctorMapper.getByKeShiId(keshiId);
    }

    @Override
    public ServerResponse delete(Integer doctorId) {
        int result = doctorMapper.deleteByPrimaryKey(doctorId);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("成功！");
        }
        return ServerResponse.createByErrorMessage("失败！");
    }
}
